package com.app.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.richfaces.component.UIDataTable;

import com.app.interfaces.interfaceService;
import com.app.model.Service;

public class ServiceBean extends MessageBean implements Serializable {
	private Service serv;
    private interfaceService servService;
    private boolean init;
    private Integer index = 0;
    private List service_list = new ArrayList();
    private String message;
    private boolean nouveau;
    private UIDataTable dataTable;

    public void viderChamps() {
        message = "";
        changerEtat();
        this.serv = new Service();
        nouveau = true;
    }

    public void annuler() {
        message = "";
        service_list = (List) getService_list();
        serv = (Service) service_list.get(index);
        changerEtat();
    }
    /** Creates a new instance of ServiceBean */
    public ServiceBean() {
        serv = new Service();
    }

    public boolean isInit(){
        message = "";
        getService_list();
        if(!(service_list.isEmpty())){
            service_list = (List)getService_list();
            serv = (Service)service_list.get(0);
        }
        etat = true;
        invetat = !etat;
        return init;
    }

    public void create(){
        if(nouveau){
            try{
                this.servService.save(this.serv);
                message = mess_insert_true;
                style_message = "Valide_message";
            } catch(Exception hx){
                hx.printStackTrace();
                message = mess_op_false;
                style_message = "err_message";
            }
        }
        else{
            try{
                this.servService.modify(serv);
                message = mess_modif_true;
                style_message = "valid_message";
            } catch(Exception hx){
                hx.printStackTrace();
                message = mess_op_false;
                style_message = "err_message";
            }
        }
        changerEtat();
    }

    public void modifierLigne(){
        this.serv = (Service)dataTable.getRowData();
        message = "";
        nouveau = false;
        changerEtat();
    }

    public void modifier(){
        message = "";
        nouveau = false;
        changerEtat();
    }

    public void supprimer(){
        try{
            this.servService.delete(serv);
            message = mess_del_true;
            style_message = "valid_message";
            if(index > 0) {
                index--;
            }
            service_list = (List)getService_list();
            if(!service_list.isEmpty()){
                serv = (Service)service_list.get(index);
            } else {
                viderChamps();
                changerEtat();
            }
        }catch(Exception hx){
            hx.printStackTrace();
            message = mess_op_false;
        }
    }



    public UIDataTable getDataTable() {
        return dataTable;
    }

    public Integer getIndex() {
        return index;
    }

    public String getMessage() {
        return message;
    }

    public boolean isNouveau() {
        return nouveau;
    }

    public Service getServ() {
        return serv;
    }

    public interfaceService getServService() {
        return servService;
    }

    public List getService_list() {
        service_list = this.servService.findAll();
        return service_list;
    }

    public void setDataTable(UIDataTable dataTable) {
        this.dataTable = dataTable;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public void setInit(boolean init) {
        this.init = init;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setNouveau(boolean nouveau) {
        this.nouveau = nouveau;
    }

    public void setServ(Service serv) {
        this.serv = serv;
    }

    public void setServService(interfaceService servService) {
        this.servService = servService;
    }

    public void setService_list(List service_list) {
        this.service_list = service_list;
    }
}
